home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Multiprocessing 2.1v2 SDK / Sample Code / MPHelloWorld / sources / MPHelloWorld.c
Encoding:
C/C++ Source or Header  |  1999-11-29  |  3.8 KB  |  135 lines  |  [TEXT/CWIE]

  1. #include <stdio.h>
  2. #include <Types.h>
  3. #include <SIOUX.h>
  4. #include <Multiprocessing.h>
  5.  
  6.  
  7. static void
  8. sendString(MPQueueID queue, char *string) {
  9. /*
  10.   Send the string through the queue one character at a time.
  11. */
  12.   while (*string) {
  13.     (void) MPNotifyQueue(queue, (void *) *string, 0, 0);
  14.     string++;
  15.   }
  16.  
  17.   (void) MPNotifyQueue(queue, (void *) '\0', 0, 0);            /* Send the final null character. */
  18. }
  19.  
  20.  
  21. static void
  22. receiveString(MPQueueID queue, char *string) {
  23. /*
  24.   Reassemble a string being transferred over the queue and return it to the caller.
  25. */
  26.   void
  27.     *first,            /* The first word of the message. */
  28.     *second,        /* The second word of the message. */
  29.     *third;            /* The third word of the message. */
  30.  
  31.   for (;;) {
  32.     (void) MPWaitOnQueue(queue, &first, &second, &third, kDurationForever);
  33.     *string = (char) first;
  34.  
  35.     if (*string == '\0') {
  36.       return;
  37.     }
  38.  
  39.     string++;
  40.   }
  41. }
  42.  
  43.  
  44. static OSStatus
  45. HelloWorld(void *parameter) {
  46.   MPQueueID
  47.     queue = parameter;
  48.  
  49.   sendString(queue, "Hello, World!");
  50.  
  51.   return noErr;
  52. }
  53.  
  54.  
  55. static int
  56. failure(char *annotation, char *routine, OSStatus status) {
  57.   printf("Uh oh.  %s%s%s returned [%d].\n",
  58.          (annotation) ? annotation : "",
  59.          (routine) ? routine : "",
  60.          (routine) ? "()" : "A routine",
  61.          status);
  62.  
  63.   return 1;
  64. }
  65.  
  66.  
  67. int
  68. main(void) {
  69.   OSStatus
  70.     status;                    /* We'll use this to test the outcome of each MP function. */
  71.   MPQueueID
  72.     terminationQueue,        /* This queue will report the completion of the task. */
  73.     communicationQueue;        /* This queue will be used to communicate between the app and the task. */
  74.   MPTaskID
  75.     task;                    /* This will be the ID of the task we create. */
  76.   char
  77.     myString[100];
  78.  
  79.   SIOUXSettings.asktosaveonclose = 0;
  80.  
  81.   if (!MPLibraryIsLoaded()) {
  82.     printf("Can't run without the \"%s\" shared library.\n", MPLibraryName);
  83.  
  84.     return 1;
  85.   }
  86.  
  87.   status = MPCreateQueue(&terminationQueue);    /* Create the queue which will report the completion of the task. */
  88.   if (status != noErr) {
  89.     return failure("Cannot create the termination queue:\n    ", "MPCreateQueue", status);
  90.   }
  91.  
  92.   status = MPCreateQueue(&communicationQueue);    /* Create the queue we'll use to communicate with. */
  93.   if (status != noErr) {
  94.     (void) MPDeleteQueue(terminationQueue);
  95.  
  96.     return failure("Cannot create the communication queue:\n    ", "MPCreateQueue", status);
  97.   }
  98.  
  99.   status = MPCreateTask(HelloWorld,                /* This is the task function. */
  100.                         communicationQueue,        /* This is the parameter to the task function. */
  101.                         0,                        /* We'll use whatever the MP system software gives us. */
  102.                         terminationQueue,        /* We'll use this to sense task completion. */
  103.                         0,                        /* We won't use the first part of the termination message. */
  104.                         0,                        /* We won't use the second part of the termination message. */
  105.                         0,                        /* Use the normal task options. (Currently this *must* be zero!) */
  106.                         &task);                    /* Here's where the ID of the new task will go. */
  107.   if (status != noErr) {
  108.     (void) MPDeleteQueue(terminationQueue);
  109.     (void) MPDeleteQueue(communicationQueue);
  110.  
  111.     return failure(0, "MPCreateTask", status);
  112.   }
  113.  
  114.   receiveString(communicationQueue, myString);
  115.   printf("%s\n", myString);
  116.  
  117.   /* Wait for the task to complete: */
  118.   status = MPWaitOnQueue(terminationQueue, 0, 0, 0, kDurationForever);
  119.   if (status != noErr) {
  120.     (void) failure("While waiting for task completion:\n    ", "MPWaitOnQueue", status);
  121.   }
  122.  
  123.   status = MPDeleteQueue(terminationQueue);
  124.   if (status != noErr) {
  125.     (void) failure("Can't delete the termination queue:\n    ", "MPDeleteQueue", status);
  126.   }
  127.  
  128.   status = MPDeleteQueue(communicationQueue);
  129.   if (status != noErr) {
  130.     (void) failure("Can't delete the communication queue:\n    ", "MPDeleteQueue", status);
  131.   }
  132.  
  133.   return 0;
  134. }
  135.